﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using BLL;
using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_EnterShapeInputs : System.Web.UI.Page
    {
        UserControl uc = null;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (SessionVariable.Instance.UserDetails.SelectedStoneTypeId > 0 &&
                SessionVariable.Instance.UserDetails.ShapeId > 0)
            {
                dvMain.Visible = true;
                LoadImage();
            }
            else
            {
                dvMain.Visible = false;

                if (SessionVariable.Instance.UserDetails.SelectedStoneTypeId <= 0)
                {
                    ShowMessage("Please select a surface first.");
                }
                else if (SessionVariable.Instance.UserDetails.ShapeId <= 0)
                {
                    ShowMessage("Please select a shape first.");
                }
            }
        }

        private void LoadImage()
        {
            //selectedImage.ImageUrl = Session["sessionObj"].ToString();
            char[] sep = new char[] { '\\' };
            string[] str = SessionVariable.Instance.UserDetails.SelectedShapeUrl.Split(sep);

            //SessionVariable.Instance.UserDetails.
            if (str[str.Length - 1].ToUpper().Contains("SHAPE1"))
            {
                uc = (Pages_Controls_Shape1)Page.LoadControl(@"~\Pages\Controls\Shape1.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);
            }
            else if (str[str.Length - 1].ToUpper().Contains("SHAPE2"))
            {
                uc = (Pages_Controls_Shape2)Page.LoadControl(@"~\Pages\Controls\Shape2.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);
            }
            else if (str[str.Length - 1].ToUpper().Contains("SHAPE3"))
            {
                uc = (Pages_Controls_Shape3)Page.LoadControl(@"~\Pages\Controls\Shape3.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);
            }
            else if (str[str.Length - 1].ToUpper().Contains("SHAPE4"))
            {
                uc = (Pages_Controls_Shape4)Page.LoadControl(@"~\Pages\Controls\Shape4.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);
            }
            else if (str[str.Length - 1].ToUpper().Contains("SHAPE5") ||
                     str[str.Length - 1].ToUpper().Contains("SHAPE6") ||
                     str[str.Length - 1].ToUpper().Contains("SHAPE7") 
                    )
            {
                //Pages_Controls_Shape5 
                uc = (Pages_Controls_Shape5)Page.LoadControl(@"~\Pages\Controls\Shape5.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);

            }
            else if (str[str.Length - 1].ToUpper().Contains("SHAPE8"))
            {
                uc = (Pages_Controls_Shape8)Page.LoadControl(@"~\Pages\Controls\Shape8.ascx");
                pnlImpageLayout.Controls.AddAt(0, uc);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadPrice();
        }

        private bool Operations()
        {
            bool retFlag = false;
            Type typeMethod = null;
            MethodInfo mInfo = null;
            typeMethod = uc.GetType();
            mInfo = typeMethod.GetMethod("OperationsUC");
            object result = mInfo.Invoke(uc, null);
            retFlag = (bool)result;
            return retFlag;
        }

        private void LoadPrice()
        {
            DAL.Price p = Common.GetPrice();

            if (p != null)
            {
                SessionVariable.Instance.UserDetails.Cost.BacksplashPrice = p.BackSplash;
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice = p.SinkCutOut;
                SessionVariable.Instance.UserDetails.Cost.Tax = p.Tax;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectShape.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Operations())
            {
                if (SessionVariable.Instance.UserDetails.IsDataPersisted)
                {
                    Response.Redirect("Estimate.aspx");
                }
                else
                {
                    if (SessionVariable.Instance.UserDetails.Shape != null &&
                        SessionVariable.Instance.UserDetails.Cost.BacksplashPrice >= 0 &&
                        SessionVariable.Instance.UserDetails.Cost.SinkoutPrice >= 0 &&
                        SessionVariable.Instance.UserDetails.Cost.Tax > 0 &&
                        SessionVariable.Instance.UserDetails.Cost.Backsplash >= 0 &&
                        SessionVariable.Instance.UserDetails.Cost.Surface > 0
                        )
                    {
                        Response.Redirect("SelectStoneCategory.aspx");
                    }
               }
            }
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                Control msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }
        }
    }
}